/**
 * @version 1.0
 * @author Ha Giang
 * @date
 */
package DiscoveryFunction;

import Communications.*;

/**
 * This class serves as a Service
 *
 * @author hhg0002
 */
public class Service {

    Multicast mc = new Multicast();
    SingleUnicastServer us;
    //the records contains servier information
    private ServiceInfo si;

    public Service() {
    }

    public Service(ServiceInfo si) {
        this.si = si;
    }

    /**
     * Register itself by announcing its information
     *
     * @return
     */
    public int registerService() {
        //start Multicast Service Channel
        mc.startMulticastServiceChannel();
        String msg = "REG " + si.toString();
        //announce 
        mc.announceMsgToServiceChannel(msg);
        //mc.closeServiceChannel();//close le qua thread chua kip chay    
        return 1;//successful
    }

    /**
     * start accepting a client
     *
     */
    public void acceptClient() {
        if (si != null) {
            us = new SingleUnicastServer(si.getPort());
            us.start();
            //System.out.print("Service:Connected");
            //us.sendMessage("I am "+si.toString()+".You are connected");
        } else {
            System.err.println("No service Info");
        }
    }

    /**
     * Get Service information
     *
     * @return ServiceInfo record
     */
    public ServiceInfo getServiceInfo() {
        return si;
    }

    /**
     * Set Service information
     *
     * @param si ServiceInfo record
     */
    public void setServiceInfo(ServiceInfo si) {
        this.si = si;
    }
}